<?php

class dbaRequestmailer
{

    function dbaRequestmailer (&$dba)
    {
        $this->dba = &$dba;
    }
    
    /** fields **/
    
    function insertField ($instance,$name,$type,$value,$description,$checking,$size,$maxlength,$selected)
    {
        $sth  = $this->dba->query("select max(pos) as maxpos from ".$this->dba->tableforwarder."_requestmailerField where instance='$instance';");
        $sth2 = $this->dba->query("select name from ".$this->dba->tableforwarder."_requestmailerField where name='$name' and instance='$instance';");
        if (is_object($sth) && is_object($sth2))
        {
            if ($sth2->num_rows())
            {
                return -1;
            }
            else 
            {
                $maxpos = $sth->fetch_row_Array();
                return $this->dba->query("insert into ".$this->dba->tableforwarder."_requestmailerField (instance,name,type,value,description,checking,size,maxlength,selected,pos) values ('$instance','$name','$type','$value','$description','$checking','$size','$maxlength','$selected','".($maxpos['maxpos']+1)."');");
            }
            
        }
    }
    
    function updateField ($instance,$name,$type,$value,$description,$checking,$size,$maxlength,$selected)
    {
        return $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerField set type='$type',value='$value',description='$description',checking='$checking',size='$size',maxlength='$maxlength',selected='$selected' where instance='$instance' and name='$name';");
    }
    
    function getallFields($instance)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerField where instance='$instance' order by pos;");
        if (is_object($sth))
        {
            $rows = $sth->fetch_rows_Array();
            $sth->free();
            return $rows;
        }
    }
    
    function getField($instance,$name)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerField where instance='$instance' and name='$name';");
        if (is_object($sth))
        {
            $row = $sth->fetch_row_Array();
            $sth->free();
            return $row;
        }
    }
    
    function exchangeFields ($instance,$name1,$name2)
    {
        $sth = $this->dba->query("select name,pos from ".$this->dba->tableforwarder."_requestmailerField where instance='$instance' and (name='$name1' or name='$name2');");
        if (is_object($sth) && ($sth->num_rows()==2))
        {
            $rows   = $sth->fetch_rows_Array();
            $byname[$rows[1]['name']] = $rows[0]['pos'];
            $byname[$rows[0]['name']] = $rows[1]['pos'];
            $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerField set pos=".$byname[$name1]." where name='$name1' and instance='$instance';");
            $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerField set pos=".$byname[$name2]." where name='$name2' and instance='$instance';");
        }
    }
    
    function deleteField ($instance,$name)
    {
        return $this->dba->query("delete from ".$this->dba->tableforwarder."_requestmailerField where name='$name' and instance='$instance';");
    }
    
    /** email data **/
    
    function insertEmailData ($instance, $to_email='', $send_ack='J', $from_email='', $reply_to_email='', $organization='', $content_type='')
    {
        return $this->dba->query("insert into ".$this->dba->tableforwarder."_requestmailerEMail (instance, to_email, send_ack, from_email, reply_to_email, organization, content_type) values ('$instance', '$to_email', '$send_ack', '$from_email', '$reply_to_email', '$organization', '$content_type');");
    }

    function updateEmailData ($instance, $to_email, $send_ack, $from_email, $reply_to_email, $organization, $content_type)
    {
        return $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerEMail set to_email='$to_email', send_ack='$send_ack', from_email='$from_email', reply_to_email='$reply_to_email', organization='$organization', content_type='$content_type' where instance='$instance';");
    }
    
    function emailDataExists ($instance)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerEMail where instance='$instance';");
        if (is_object($sth))
        {
            if ($sth->num_rows())
            {
                $sth->free();
                return true;
            }
        }
    }

    function getEmailData ($instance)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerEMail where instance='$instance' limit 1;");
        if (is_object($sth))
        {
            $row = $sth->fetch_row_Array();
            $sth->free();
            return $row;
        }
    }
    
    /** aditional **/
    
    function aditionalExists ($instance)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerAditional where instance='$instance';");
        if (is_object($sth))
        {
            if ($sth->num_rows())
            {
                $sth->free();
                return true;
            }
        }
    }

    function createAditional ($instance)
    {
        $this->dba->query("insert into ".$this->dba->tableforwarder."_requestmailerAditional (instance) values ('$instance')");
    }

    function updateAditional ($instance,$errormsg,$finscreen)
    {
        return $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerAditional set errormsg='$errormsg',finscreen='$finscreen' where instance='$instance';");
    }
    
    function getAditional ($instance)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerAditional where instance='$instance';");
        if (is_object($sth))
        {
            $row = $sth->fetch_row_Array();
            $sth->free();
            return $row;
        }
    }

    /** mail templates **/

    function tplExists ($instance)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerTpl where instance='$instance';");
        if (is_object($sth))
        {
            if ($sth->num_rows())
            {
                $sth->free();
                return true;
            }
        }
    }

    function createTpls ($instance)
    {
        $this->dba->query("insert into ".$this->dba->tableforwarder."_requestmailerTpl (instance,type) values ('$instance','form')");
        $this->dba->query("insert into ".$this->dba->tableforwarder."_requestmailerTpl (instance,type) values ('$instance','ack')");
    }

    function updateTpl ($instance,$type,$subject,$tpl)
    {
        return $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerTpl set subject='$subject', tpl='$tpl' where instance='$instance' and type='$type';");
    }
    
    function getTpl ($instance,$type)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerTpl where instance='$instance' and type='$type';");
        if (is_object($sth))
        {
            $row = $sth->fetch_row_Array();
            $sth->free();
            return $row;
        }
    }

/** FieldTpls **/

    function insertFieldTpl ($name,$tpl)
    {
        return $this->dba->query("insert into ".$this->dba->tableforwarder."_requestmailerFieldTpl (name,tpl) values ('$name','$tpl');");
    }
    
    function updateFieldTpl ($id,$name,$tpl)
    {
        return $this->dba->query("update ".$this->dba->tableforwarder."_requestmailerFieldTpl set name='$name',tpl='$tpl' where id=$id;");
    }
    
    function getallFieldTpls()
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerFieldTpl order by name;");
        if (is_object($sth))
        {
            $rows = $sth->fetch_rows_Array();
            $sth->free();
            return $rows;
        }
    }
    
    function getFieldTplNames()
    {
        $sth = $this->dba->query("select id,name from ".$this->dba->tableforwarder."_requestmailerFieldTpl order by name;");
        if (is_object($sth))
        {
            $rows = $sth->fetch_rows_Array();
            $sth->free();
            return $rows;
        }
    }
    
    function getFieldTpl($id)
    {
        $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_requestmailerFieldTpl where id=$id;");
        if (is_object($sth))
        {
            $row = $sth->fetch_row_Array();
            $sth->free();
            return $row;
        }
    }
    
    function deleteFieldTpl ($id)
    {
        return $this->dba->query("delete from ".$this->dba->tableforwarder."_requestmailerFieldTpl where id=$id;");
    }
    


}

?>
